package com.example.computer_exam_training_sys.service.impl;

import com.example.computer_exam_training_sys.dto.StudyNoteDTO;
import com.example.computer_exam_training_sys.entity.UserStudyNotes;
import com.example.computer_exam_training_sys.mapper.UserStudyNotesMapper;
import com.example.computer_exam_training_sys.service.UserStudyNotesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserStudyNotesServiceImpl implements UserStudyNotesService {

    @Autowired
    private UserStudyNotesMapper notesMapper;

    @Override
    public List<StudyNoteDTO> getUserNotes(Long userId) {
        List<UserStudyNotes> notes = notesMapper.findByUserId(userId);
        return notes.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public List<StudyNoteDTO> getQuestionNotes(Long questionId) {
        List<UserStudyNotes> notes = notesMapper.findByQuestionId(questionId);
        return notes.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional
    public StudyNoteDTO createNote(Long userId, Long questionId, String content) {
        UserStudyNotes note = new UserStudyNotes();
        note.setUserId(userId);
        note.setQuestionId(questionId);
        note.setContent(content);
        note.setIsPublic(false);
        note.setCreateTime(LocalDateTime.now());
        note.setUpdateTime(LocalDateTime.now());
        
        notesMapper.insert(note);
        return convertToDTO(note);
    }

    @Override
    @Transactional
    public StudyNoteDTO updateNote(Long noteId, String content, Long userId) {
        UserStudyNotes note = notesMapper.findById(noteId);
        if (note != null && note.getUserId().equals(userId)) {
            note.setContent(content);
            note.setUpdateTime(LocalDateTime.now());
            notesMapper.update(note);
            return convertToDTO(note);
        }
        return null;
    }

    @Override
    @Transactional
    public void shareNote(Long noteId, Long senderId, Long receiverId) {
        UserStudyNotes note = notesMapper.findById(noteId);
        if (note != null && note.getUserId().equals(senderId)) {
            // 创建分享记录
            notesMapper.insertShareRecord(noteId, senderId, receiverId);
        }
    }

    @Override
    public StudyNoteDTO getSharedNote(Long noteId, Long userId) {
        // 首先验证用户是否有权限查看该笔记
        UserStudyNotes note = notesMapper.findSharedNoteById(noteId, userId);
        if (note == null) {
            throw new RuntimeException("笔记不存在或无权访问");
        }
        return convertToDTO(note);
    }

    @Override
    public List<StudyNoteDTO> getReceivedSharedNotes(Long userId) {
        List<UserStudyNotes> notes = notesMapper.findReceivedSharedNotes(userId);
        return notes.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public List<StudyNoteDTO> getSentSharedNotes(Long userId) {
        List<UserStudyNotes> notes = notesMapper.findSentSharedNotes(userId);
        return notes.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public List<StudyNoteDTO> getAllNotes(Long adminId, Long userId) {
        // 如果是管理员，返回所有笔记
        if (adminId != null) {
            List<UserStudyNotes> notes = notesMapper.findAllNotes();
            return notes.stream()
                    .map(this::convertToDTO)
                    .collect(Collectors.toList());
        }
        // 如果是普通用户，只返回自己的笔记
        return getUserNotes(userId);
    }

    @Override
    @Transactional
    public void deleteNote(Long noteId, Long userId) {
        UserStudyNotes note = notesMapper.findById(noteId);
        if (note != null && note.getUserId().equals(userId)) {
            notesMapper.delete(noteId);
        }
    }

    private StudyNoteDTO convertToDTO(UserStudyNotes note) {
        StudyNoteDTO dto = new StudyNoteDTO();
        dto.setNoteId(note.getNoteId());
        dto.setUserId(note.getUserId());
        dto.setQuestionId(note.getQuestionId());
        dto.setContent(note.getContent());
        dto.setTags(note.getTags());
        dto.setIsPublic(note.getIsPublic());
        dto.setIsShared(note.getIsShared());
        dto.setCreateTime(note.getCreateTime());
        dto.setUpdateTime(note.getUpdateTime());
        dto.setUserName(note.getUserName());
        dto.setQuestionTitle(note.getQuestionTitle());
        return dto;
    }
} 